﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BookStore.Data
{
    public class FAQController
    {
        static SqlConnection con = sqlDataProvider.getConnect();
        static DataTable dt;
        #region getbyID
        public static FAQInfo getbyID(int faqID)
        {
            FAQInfo objFAQ = new FAQInfo();
            using (SqlCommand cmd = new SqlCommand("getIDFAQ", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@faqID", faqID));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        objFAQ.faqID = Convert.ToInt32(dr.GetInt32(0));
                        objFAQ.faqName = dr.GetString(1);
                        objFAQ.faqContent = dr.GetString(2);
                    }
                }
                dr.Close();
            }
            return objFAQ;
        }

        #endregion

        #region getbyAll
        public static DataTable ListFAQ()
        {
            using (SqlDataAdapter da = new SqlDataAdapter("listFAQ", con))
            {
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                dt = new DataTable();
                da.Fill(dt);
            }
            return dt;
        }
        #endregion

        #region insert
        public static bool insertFAQ(FAQInfo objFAQ)
        {
            using (SqlCommand cmd = new SqlCommand("insertFAQ", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@faqName", objFAQ.faqName));
                cmd.Parameters.Add(new SqlParameter("@faqContent", objFAQ.faqContent));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region update
        public static bool updateFAQ(FAQInfo objFAQ)
        {
            using (SqlCommand cmd = new SqlCommand("updateFAQ", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@faqID", objFAQ.faqID));
                cmd.Parameters.Add(new SqlParameter("@faqName", objFAQ.faqName));
                cmd.Parameters.Add(new SqlParameter("@faqContent", objFAQ.faqContent));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion

        #region delete
        public static bool deleteFAQ(int faqID)
        {
            using (SqlCommand cmd = new SqlCommand("deleteFAQ", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@faqID", faqID));
                cmd.ExecuteNonQuery();
            }
            return true;
        }
        #endregion
    }
}
